/*
 * adc-ss805x.c

 * ADC driver for SS805X.
 *
 * Copyright (C) 2024 Sinh Micro, Inc.
 * Subject to the GNU Public License, version 2.
 *
 * Author: lixiang<lixiang@sinhmicro.com>
 * 
 * Encoding format: GB2312
 * Version: v1.2.2
 * Date: 2024-11-05
 */
 
#include "hal-config.h"
#include "hal-regs.h"

#include "types.h"
#include "adc.h"

#if (defined(CONFIG_SYS_CHIP_SERIES_SS805X))

#define ADC_DBG0    (*(volatile unsigned char xdata*)(0xFEE6))

uint16_t adc_compute(uint16_t compute_val)
{
    uint32_t tmp;

    tmp = (uint32_t)compute_val * 500;
    compute_val = tmp / 512 - 100;

    return compute_val;
}

uint16_t adc_get_value(uint8_t channel_num)
{
    uint16_t val;
     
    ADCCHSEL = channel_num;
    
    val = ADCDATL;
    val += (ADCDATH << 8);

    return val;
}

uint16_t adc_get_value_mv(uint8_t channel_num)
{
    return adc_compute(adc_get_value(channel_num));
}

uint16_t adc_get_vbat(void)
{
    return adc_compute(adc_get_value(0x11)) << 1;
}

#endif